package com.controller.manage;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.po.Adminmain;
import com.service.manage.AdminService;
import com.service.manage.imp.SessionListener;
import com.util.ForceLogout;
import com.util.MD5Util;
import com.util.StringFinal;

@Controller
@RequestMapping("/jsp/manager")
public class AdminController {

	@Resource(name="adminServiceImp")
	public AdminService adminService;
	
	@ResponseBody
	@RequestMapping(value="/adminlogin")
	public int isLogin(String username,String password,Model model,HttpServletRequest request){
		HttpSession session = request.getSession();
		
		Adminmain admin = new Adminmain();
		admin.setAdminid(username);
		admin.setAdminpwd(password);
		boolean result = adminService.getAdmin(admin);
		int rescode = 0;
		if(result) {
			ForceLogout.forceLogout(username, session);
			SessionListener.user_session.put(admin.getAdminid(), session);
			session.setAttribute("admin", admin);
			//model.addAttribute("loginid", admin.getAdminid());
			rescode=1;
		}
		return rescode;
	}
	
	@ResponseBody
	@RequestMapping(value="/checkAdmin")
	public int checkAmin(String adminid){
		Adminmain admin = new Adminmain();
		admin.setAdminid(adminid);
		boolean result = adminService.getAdmin(admin);
		int rescode=0;
		if(result)
			rescode = 1;
		return rescode;
	}
	
	@ResponseBody
	@RequestMapping(value="/getAdminList")
	public Map<String, Object> getAdminList(Adminmain admin,Integer page,Integer rows){
		admin.setPage((page-1)*rows);
		admin.setRows(rows);
		List<Adminmain> list = adminService.getAdminList(admin);
		int count = adminService.getAdminCount(admin);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("total", count); //存放总的记录
		map.put("rows", list);  //存放每页记录数
		return map;
	}
	
	@ResponseBody
	@RequestMapping(value="/addAdmin")
	public int addAdmin(Adminmain admin){
		admin.setAdminvalidate(StringFinal.validateStr);
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-dd");
		admin.setAdmindate(format.format(date));
		admin.setAdminpwd(MD5Util.getMD5(StringFinal.primaryStr));
		int result = adminService.addAdmin(admin);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value="/updateAdmin")
	public int updateAdmin(Adminmain admin){
		int result = adminService.updateAdmin(admin);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value="/delAdmin")
	public int delAdmin(Adminmain admin){
		
		int result = adminService.delAdmin(admin);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value="/delAdminList")
	public int delAdminList(String adminids){
		
		String[] array = adminids.split(",");
		int result = adminService.delAdminList(array);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value="/updateAdpwd")
	public int updateAdminpwd(Adminmain admin){
		admin.setAdminpwd(MD5Util.getMD5(admin.getAdminpwd()));
		int result = adminService.updateAdminpwd(admin);
		return result;
	}
	
	@RequestMapping(value="/logout")
	public void logout(Adminmain admin,HttpServletRequest request,HttpServletResponse resp) throws IOException{
		
		HttpSession session = request.getSession();
		session.removeAttribute("admin");
		String contextPath = session.getServletContext().getContextPath();
		SessionListener.user_session.remove(admin.getAdminid());
		String fpath=contextPath+"/jsp/manager/login.jsp";
		resp.sendRedirect(fpath);
		
	}
	
	

	 
}
